﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Net.Mail;


namespace mpsalary
{
    public partial class mainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

          
        }

        protected void tbxSignIn_Click(object sender, EventArgs e)
        {
            
            Session["Name"] = tbxUsername.Text;

           //Login 
            mpsalary.App_Code.User u = new mpsalary.App_Code.User();

            u.NRIC = tbxUsername.Text;
            u.RetrievePassword();
            if (tbxPassword.Text == u.Password)
            {
                Response.Redirect("homePage.aspx");
            }
            else
            {
                lblerror.Text = "Sorry, you entered the wrong password";
            }
        }


        protected void tbxSignUp_Click(object sender, EventArgs e)
        {
            //Sign Up
            mpsalary.App_Code.User u = new mpsalary.App_Code.User();

            //retrieve from text boxes
            u.NRIC = tbxName.Text;
            u.Email = tbxEmailAddress.Text;
            u.Password = tbxSignUpPassword.Text;
            // user created
            u.CreateUser();

            // Email will be sent after sign up
            try
            {
                MailMessage email = new MailMessage();
                email.From = new MailAddress("Fromthepgsteam@gmail.com", "PGS Portal");
                email.To.Add(new MailAddress(tbxEmailAddress.Text));
                email.Subject = "Welcome to PGS Portal";
                email.Body = "<html><body>Hello,<br><br>Thank You for signing up with PGS Portal.<br></body></html>";
                email.IsBodyHtml = true;
                email.Priority = MailPriority.Normal;

                SmtpClient mailclient = new SmtpClient("smtp.gmail.com");
                mailclient.Credentials = new System.Net.NetworkCredential("thepgsteam@gmail.com", "thepgsteam");
                mailclient.Port = 587;
                mailclient.EnableSsl = true;
                mailclient.Send(email);

               // lblemail.Text = "email sent";
            }
            catch
            {
               // lblemail.Text = "Sending of mail failed";
            }

            
            Response.Redirect("Signup.aspx");


        }
        

    }
}
